// forEach
const arr = [1, 2, 3];
const testArr = ['a', 'b']

// 1. forEach
// arr.forEach((value, index, arr) => {
//     console.log(value);
// });
Array.prototype.myForEach = function (callback, thisArg) {
    const len = this.length;
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            callback.call(thisArg, this[i], i, this);
        }
    }
}
console.log("1. forEach");
arr.myForEach((value, index, arr) => {
    console.log(value, index, arr);
}, testArr)


// 2. map
// const mapResult = arr.map((item, index, arr) => {
//     return item *= index;
// })
// console.log(mapResult);

Array.prototype.myMap = function (callback, thisArg) {
    const len = this.length;
    let result = [];
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            result[i] = callback.call(thisArg, this[i], i, this);
        }
    }
    return result;
}

Array.prototype.myMap = function (callback, thisArg) {
    const len = this.length;
    let result = [];
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            result[i] = callback.call(thisArg, this[i], i, this);
        }
    }
    return result;
}

const myMapResult = arr.myMap((item) => {
    return item * 2;
})
console.log("2. map");
console.log(myMapResult);

// 3. filter
// const filterResult = arr.filter(item => item > 2);
// console.log(filterResult);

Array.prototype.myFilter = function (callback, thisArg) {
    const len = this.length;
    const res = [];
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            if (callback.call(thisArg, this[i], i, this))
                res.push(this[i]);
        }
    }
    return res;
}
console.log("3. filter");
const myFilterResult = arr.myFilter(item => item > 2);
console.log(myFilterResult);

// 4. every
// console.log(arr.every(item => item > 0));
Array.prototype.myEvery = function (callback, thisArg) {
    const len = this.length;
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            if (!callback.call(thisArg, this[i], i, this)) {
                return false;
            }
        }
    }
    return true;
}

console.log("4. every");
console.log(arr.myEvery(item => item > 1));


// 5. some
// console.log(arr.some(item => item > 2));
Array.prototype.mySome = function (callback, thisArg) {
    const len = this.length;
    for (let i = 0; i < len; i++) {
        if (Object.prototype.hasOwnProperty.call(this, i)) {
            if (callback.call(thisArg, this[i], i, this)) {
                return true;
            }
        }
    }
    return false;
}

console.log("5. some");
console.log(arr.mySome(item => item > 2));

// 6. reduce
// const reduceResult = arr.reduce((prev, cur) => prev += cur, 10);
// console.log(reduceResult);

Array.prototype.myReduce = function (callback, initValue) {
    const len = this.length;
    for (let i = 0; i < len; i++) {
        console.log(initValue);
        initValue = callback(initValue, this[i]);
    }
    return initValue;
}

console.log("6. reduce");
const myReduceResult = arr.myReduce((prev, cur) => prev += cur, 10);
console.log(myReduceResult);

Array.prototype.myReduce = function(callback, initValue) {
    const len = this.length;
    for (let i = 0; i < len; i++) {
        initValue = callback(initValue, this[i]);
    }   
    return initValue;
}



function A(x) {
    this.x = x;
}
A.prototype.x = 1;
function B(x) {
    this.x = x;
}
B.prototype = new A();
const b = new B(3);
console.log(b.x);
delete b.x;
console.log(b.x)

console.log(NaN === NaN, !!'', !![]);